home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / doom / quake.zip / HIPGRAPL.ZIP / AI.QC next >
Text File  |  1997-02-06  |  25KB  |  1,201 lines

  1. void() movetarget_f;
  2. void() t_movetarget;
  3. void() knight_walk1;
  4. void() knight_bow6;
  5. void() knight_bow1;
  6. void(entity etemp, entity stemp, entity stemp, float dmg) T_Damage;
  7. /*
  8.  
  9. .enemy
  10. Will be world if not currently angry at anyone.
  11.  
  12. .movetarget
  13. The next path spot to walk toward.  If .enemy, ignore .movetarget.
  14. When an enemy is killed, the monster will try to return to it's path.
  15.  
  16. .huntt_ime
  17. Set to time + something when the player is in sight, but movement straight for
  18. him is blocked.  This causes the monster to use wall following code for
  19. movement direction instead of sighting on the player.
  20.  
  21. .ideal_yaw
  22. A yaw angle of the intended direction, which will be turned towards at up
  23. to 45 deg / state.  If the enemy is in view and hunt_time is not active,
  24. this will be the exact line towards the enemy.
  25.  
  26. .pausetime
  27. A monster will leave it's stand state and head towards it's .movetarget when
  28. time > .pausetime.
  29.  
  30. walkmove(angle, speed) primitive is all or nothing
  31. */
  32.  
  33.  
  34. //
  35. // globals
  36. //
  37. float    current_yaw;
  38.  
  39. //
  40. // when a monster becomes angry at a player, that monster will be used
  41. // as the sight target the next frame so that monsters near that one
  42. // will wake up even if they wouldn't have noticed the player
  43. //
  44. entity    sight_entity;
  45. float    sight_entity_time;
  46. void() FoundTarget;
  47.  
  48. float(float v) anglemod =
  49. {
  50.     while (v >= 360)
  51.         v = v - 360;
  52.     while (v < 0)
  53.         v = v + 360;
  54.     return v;
  55. };
  56.  
  57. /*
  58. ==============================================================================
  59.  
  60. MOVETARGET CODE
  61.  
  62. The angle of the movetarget effects standing and bowing direction, but has no effect on movement, which allways heads to the next target.
  63.  
  64. targetname
  65. must be present.  The name of this movetarget.
  66.  
  67. target
  68. the next spot to move to.  If not present, stop here for good.
  69.  
  70. pausetime
  71. The number of seconds to spend standing or bowing for path_stand or path_bow
  72.  
  73. ==============================================================================
  74. */
  75.  
  76.  
  77. void() movetarget_f =
  78. {
  79.     if (!self.targetname)
  80.         objerror ("monster_movetarget: no targetname");
  81.  
  82.     self.solid = SOLID_TRIGGER;
  83.     self.touch = t_movetarget;
  84.     setsize (self, '-8 -8 -8', '8 8 8');
  85.  
  86. };
  87.  
  88. /*QUAKED path_corner (0.5 0.3 0) (-8 -8 -8) (8 8 8)
  89. Monsters will continue walking towards the next target corner.
  90. "delay" delay to wait before proceeding to next segment;
  91. */
  92. void() path_corner =
  93. {
  94.     movetarget_f ();
  95. };
  96.  
  97.  
  98. /*
  99. =============
  100. t_movetarget
  101.  
  102. Something has bumped into a movetarget.  If it is a monster
  103. moving towards it, change the next destination and continue.
  104. ==============
  105. */
  106. void() t_movetarget =
  107. {
  108. local entity    temp;
  109.  
  110.     if (other.movetarget != self)
  111.         return;
  112.  
  113.     if (other.enemy)
  114.         return;        // fighting, not following a path
  115.  
  116.     temp = self;
  117.     self = other;
  118.     other = temp;
  119.  
  120.     if (self.classname == "monster_ogre")
  121.         sound (self, CHAN_VOICE, "ogre/ogdrag.wav", 1, ATTN_IDLE);// play chainsaw drag sound
  122.  
  123. //dprint ("t_movetarget\n");
  124. //MED
  125.    if (other.target)
  126.       {
  127.       self.goalentity = self.movetarget = find (world, targetname, other.target);
  128.       self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  129.       if (!self.movetarget)
  130.          {
  131.          self.pausetime = time + 999999;
  132.          self.th_stand ();
  133.          return;
  134.          }
  135.    //MED 01/20/97
  136.       else if (other.delay)
  137.          {
  138.          self.pausetime = time + other.delay;
  139.          self.th_stand ();
  140.          }
  141.       }
  142.    else
  143.       {
  144.       self.pausetime = time + 999999;
  145.       self.th_stand ();
  146.       return;
  147.       }
  148. };
  149.  
  150.  
  151. //MED 01/20/97
  152.  
  153. /*
  154. =============
  155. t_followtarget
  156.  
  157. Something has bumped into a followtarget.  If it is a monster
  158. moving towards it, change the next destination and continue.
  159. ==============
  160. */
  161. void() t_followtarget =
  162. {
  163.    local entity   temp;
  164.    local vector   spot1, spot2;
  165.    local entity   targ;
  166.    local entity   client;
  167.  
  168.    if (!other.flags & FL_MONSTER)
  169.       return;
  170.    if (other.classname == "monster_decoy")
  171.       return;
  172. //   if (other.enemy == world)
  173. //      return;
  174.    if (other.wetsuit_time > time)
  175.       return;
  176.    targ = other.enemy;
  177.  
  178. // see if any entities are in the way of the shot
  179.    spot1 = other.origin + other.view_ofs;
  180.     spot2 = targ.origin + targ.view_ofs;
  181.  
  182.    traceline (spot1, spot2, FALSE, other);
  183.    if (trace_fraction == 1)
  184.       return;
  185.  
  186.     if (other.enemy)
  187.       {
  188.       // make the monster tame
  189.       other.oldenemy = other.enemy;
  190.       other.enemy = world;
  191.       other.think = other.th_walk;
  192.       }
  193.  
  194.     temp = self;
  195.     self = other;
  196.     other = temp;
  197.  
  198.     self.goalentity = self.movetarget = find (world, targetname, other.target);
  199. /*
  200.    bprint("target ");
  201.    bprint(other.target);
  202.    bprint(" targetname ");
  203.    bprint(self.goalentity.targetname);
  204.    bprint(" origin ");
  205.    bprint(vtos(self.goalentity.origin));
  206.    bprint("\n");
  207. */
  208.    self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  209.    self.wetsuit_time = time + 2;
  210.     if (!self.movetarget)
  211.       {
  212.       if (self.oldenemy != world)
  213.          {
  214.          self.enemy = self.oldenemy;
  215.          FoundTarget();
  216.          return;
  217.          }
  218.       else
  219.          {
  220.          client = checkclient ();
  221.          if (!client)
  222.             {
  223.             self.enemy = client;
  224.             FoundTarget();
  225.             return;
  226.             }
  227.          self.pausetime = time + 999999;
  228.          self.th_stand ();
  229.          }
  230.       }
  231. };
  232.  
  233. void() followtarget_f =
  234. {
  235. //   if (!self.targetname)
  236. //      objerror ("monster_followtarget: no targetname");
  237.  
  238.     self.solid = SOLID_TRIGGER;
  239.    self.touch = t_followtarget;
  240.    setmodel (self, self.model);  // set size and link into world
  241.     self.movetype = MOVETYPE_NONE;
  242.     self.modelindex = 0;
  243.     self.model = "";
  244. //   setsize (self, '-8 -8 -8', '8 8 8');
  245.  
  246. };
  247.  
  248. /*QUAKED path_follow (0.5 0.3 0) ?
  249. Monsters will stop what they are doing and follow to the path
  250. */
  251.  
  252. void() path_follow =
  253. {
  254.    followtarget_f ();
  255. };
  256.  
  257. /*QUAKED path_follow2 (0.5 0.3 0) (-8 -8 -8) (8 8 8)
  258. Monsters will stop what they are doing and follow to the path
  259. */
  260.  
  261. void() path_follow2 =
  262. {
  263.    self.solid = SOLID_TRIGGER;
  264.    self.touch = t_followtarget;
  265.     setsize (self, '-8 -8 -8', '8 8 8');
  266. };
  267.  
  268.  
  269. //============================================================================
  270.  
  271. /*
  272. =============
  273. range
  274.  
  275. returns the range catagorization of an entity reletive to self
  276. 0    melee range, will become hostile even if back is turned
  277. 1    visibility and infront, or visibility and show hostile
  278. 2    infront and show hostile
  279. 3    only triggered by damage
  280. =============
  281. */
  282. float(entity targ) range =
  283. {
  284. local vector    spot1, spot2;
  285. local float        r;
  286.     spot1 = self.origin + self.view_ofs;
  287.     spot2 = targ.origin + targ.view_ofs;
  288.  
  289.     r = vlen (spot1 - spot2);
  290.     if (r < 120)
  291.         return RANGE_MELEE;
  292.     if (r < 500)
  293.         return RANGE_NEAR;
  294.     if (r < 1000)
  295.         return RANGE_MID;
  296.     return RANGE_FAR;
  297. };
  298.  
  299. /*
  300. =============
  301. visible
  302.  
  303. returns 1 if the entity is visible to self, even if not infront ()
  304. =============
  305. */
  306. float (entity targ) visible =
  307. {
  308.     local vector    spot1, spot2;
  309.  
  310.     spot1 = self.origin + self.view_ofs;
  311.     spot2 = targ.origin + targ.view_ofs;
  312.    traceline (spot1, spot2, TRUE, self);  // see through other monsters
  313.  
  314.     if (trace_inopen && trace_inwater)
  315.         return FALSE;            // sight line crossed contents
  316.  
  317. //MED 11/21/96
  318.     if (trace_fraction == 1)
  319.       {
  320.       visible_distance = vlen(spot2-spot1);
  321.       return TRUE;
  322.       }
  323.     return FALSE;
  324. };
  325.  
  326.  
  327. /*
  328. =============
  329. infront
  330.  
  331. returns 1 if the entity is in front (in sight) of self
  332. =============
  333. */
  334. float(entity targ) infront =
  335. {
  336.     local vector    vec;
  337.     local float        dot;
  338.  
  339.     makevectors (self.angles);
  340.     vec = normalize (targ.origin - self.origin);
  341.     dot = vec * v_forward;
  342.  
  343.     if ( dot > 0.3)
  344.     {
  345.         return TRUE;
  346.     }
  347.     return FALSE;
  348. };
  349.  
  350.  
  351. //============================================================================
  352.  
  353. /*
  354. ===========
  355. ChangeYaw
  356.  
  357. Turns towards self.ideal_yaw at self.yaw_speed
  358. Sets the global variable current_yaw
  359. Called every 0.1 sec by monsters
  360. ============
  361. */
  362. /*
  363.  
  364. void() ChangeYaw =
  365. {
  366.     local float        ideal, move;
  367.  
  368. //current_yaw = self.ideal_yaw;
  369. // mod down the current angle
  370.     current_yaw = anglemod( self.angles_y );
  371.     ideal = self.ideal_yaw;
  372.  
  373.     if (current_yaw == ideal)
  374.         return;
  375.  
  376.     move = ideal - current_yaw;
  377.     if (ideal > current_yaw)
  378.     {
  379.         if (move > 180)
  380.             move = move - 360;
  381.     }
  382.     else
  383.     {
  384.         if (move < -180)
  385.             move = move + 360;
  386.     }
  387.  
  388.     if (move > 0)
  389.     {
  390.         if (move > self.yaw_speed)
  391.             move = self.yaw_speed;
  392.     }
  393.     else
  394.     {
  395.         if (move < 0-self.yaw_speed )
  396.             move = 0-self.yaw_speed;
  397.     }
  398.  
  399.     current_yaw = anglemod (current_yaw + move);
  400.  
  401.     self.angles_y = current_yaw;
  402. };
  403.  
  404. */
  405.  
  406. //MED 10/18/96 added charmed stuff
  407. //============================================================================
  408. void() UpdateCharmerGoal =
  409.    {
  410.    local entity targ;
  411.    local vector d;
  412.  
  413.    d = normalize(self.origin-self.charmer.origin);
  414.  
  415.    if (self.huntingcharmer == 1)
  416.       {
  417.       targ = spawn();
  418.       self.trigger_field = targ;
  419.       setorigin(targ,self.charmer.origin);
  420.       self.huntingcharmer = 2;
  421.       self.goalentity = targ;
  422.       }
  423.    if (self.huntingcharmer == 2)
  424.       {
  425.       targ = self.trigger_field;
  426.       traceline(self.origin,self.charmer.origin,TRUE,self);
  427.       if (trace_fraction == 1.0)
  428.          {
  429. //         dprint("can see ");
  430. //         dprint(vtos(self.charmer.origin));
  431. //         dprint("\n");
  432.          setorigin(targ,self.charmer.origin);
  433.          }
  434.       }
  435.    else
  436.       {
  437.       targ = self.trigger_field;
  438.       setorigin(targ,self.charmer.origin + (d*300));
  439.       }
  440.    };
  441.  
  442. //============================================================================
  443. void() HuntCharmer =
  444. {
  445.    self.huntingcharmer = 1;
  446.    UpdateCharmerGoal();
  447. //   self.goalentity = self.charmer;
  448.    self.think = self.th_walk;
  449.    self.ideal_yaw = vectoyaw(self.goalentity.origin - self.origin);
  450.    self.nextthink = time + 0.1;
  451. };
  452.  
  453. //============================================================================
  454. void() FleeCharmer =
  455. {
  456.    self.huntingcharmer = 1;
  457.    UpdateCharmerGoal();
  458.    self.huntingcharmer = 3;
  459. //   self.goalentity = self.charmer;
  460.    self.think = self.th_walk;
  461. //   self.ideal_yaw = vectoyaw(self.originself.goalentity.origin - self.origin);
  462.    self.nextthink = time + 0.1;
  463. };
  464.  
  465. //============================================================================
  466. void() StopHuntingCharmer =
  467. {
  468.    self.goalentity = world;
  469.    if (self.huntingcharmer>1)
  470.       remove(self.trigger_field);
  471.    self.huntingcharmer = 0;
  472.    self.think = self.th_stand;
  473.    self.nextthink = time + 0.1;
  474. };
  475.  
  476. //============================================================================
  477.  
  478. void() HuntTarget =
  479. {
  480.     self.goalentity = self.enemy;
  481.     self.think = self.th_run;
  482.     self.ideal_yaw = vectoyaw(self.enemy.origin - self.origin);
  483.     self.nextthink = time + 0.1;
  484.     SUB_AttackFinished (1);    // wait a while before first attack
  485. };
  486.  
  487. void() SightSound =
  488. {
  489. local float    rsnd;
  490.  
  491.     if (self.classname == "monster_ogre")
  492.         sound (self, CHAN_VOICE, "ogre/ogwake.wav", 1, ATTN_NORM);
  493.     else if (self.classname == "monster_knight")
  494.         sound (self, CHAN_VOICE, "knight/ksight.wav", 1, ATTN_NORM);
  495.     else if (self.classname == "monster_shambler")
  496.         sound (self, CHAN_VOICE, "shambler/ssight.wav", 1, ATTN_NORM);
  497.     else if (self.classname == "monster_demon1")
  498.         sound (self, CHAN_VOICE, "demon/sight2.wav", 1, ATTN_NORM);
  499.     else if (self.classname == "monster_wizard")
  500.         sound (self, CHAN_VOICE, "wizard/wsight.wav", 1, ATTN_NORM);
  501.     else if (self.classname == "monster_zombie")
  502.         sound (self, CHAN_VOICE, "zombie/z_idle.wav", 1, ATTN_NORM);
  503.     else if (self.classname == "monster_dog")
  504.         sound (self, CHAN_VOICE, "dog/dsight.wav", 1, ATTN_NORM);
  505.     else if (self.classname == "monster_hell_knight")
  506.         sound (self, CHAN_VOICE, "hknight/sight1.wav", 1, ATTN_NORM);
  507.     else if (self.classname == "monster_tarbaby")
  508.         sound (self, CHAN_VOICE, "blob/sight1.wav", 1, ATTN_NORM);
  509.     else if (self.classname == "monster_vomit")
  510.         sound (self, CHAN_VOICE, "vomitus/v_sight1.wav", 1, ATTN_NORM);
  511.     else if (self.classname == "monster_enforcer")
  512.     {
  513.         rsnd = rint(random() * 3);
  514.         if (rsnd == 1)
  515.             sound (self, CHAN_VOICE, "enforcer/sight1.wav", 1, ATTN_NORM);
  516.         else if (rsnd == 2)
  517.             sound (self, CHAN_VOICE, "enforcer/sight2.wav", 1, ATTN_NORM);
  518.         else if (rsnd == 0)
  519.             sound (self, CHAN_VOICE, "enforcer/sight3.wav", 1, ATTN_NORM);
  520.         else
  521.             sound (self, CHAN_VOICE, "enforcer/sight4.wav", 1, ATTN_NORM);
  522.     }
  523.     else if (self.classname == "monster_army")
  524.         sound (self, CHAN_VOICE, "soldier/sight1.wav", 1, ATTN_NORM);
  525.     else if (self.classname == "monster_shalrath")
  526.         sound (self, CHAN_VOICE, "shalrath/sight.wav", 1, ATTN_NORM);
  527. //MED
  528.    else if (self.classname == "monster_gremlin")
  529.       {
  530.       if (self.stoleweapon == 0)
  531.          sound (self, CHAN_VOICE, "grem/sight1.wav", 1, ATTN_NORM);
  532.       }
  533. //MED
  534.    else if (self.classname == "monster_scourge")
  535.       sound (self, CHAN_VOICE, "scourge/sight.wav", 1, ATTN_NORM);
  536. //MED
  537.    else if (self.classname == "monster_armagon")
  538.       sound (self, CHAN_VOICE, "armagon/sight.wav", 1, 0.1);
  539. };
  540.  
  541. void() FoundTarget =
  542. {
  543. //MED
  544.     if (self.enemy.classname == "player")
  545.       {
  546.       if (self.charmed)
  547.          {
  548.          if ((self.charmer == self.enemy) || (self.charmer == self.enemy.charmer))
  549.             {
  550.             self.enemy = world;
  551.             return;
  552.             }
  553.          }
  554.       // let other monsters see this monster for a while
  555.         sight_entity = self;
  556.         sight_entity_time = time;
  557.       }
  558.    else if (self.charmed)
  559.       {
  560.       if ((self.charmer == self.enemy.charmer))
  561.          {
  562.          self.enemy = world;
  563.          return;
  564.          }
  565.       }
  566.  
  567.     self.show_hostile = time + 1;        // wake up other monsters
  568.  
  569.     SightSound ();
  570.     HuntTarget ();
  571. };
  572.  
  573. /*
  574. ===========
  575. FindTarget
  576.  
  577. Self is currently not attacking anything, so try to find a target
  578.  
  579. Returns TRUE if an enemy was sighted
  580.  
  581. When a player fires a missile, the point of impact becomes a fakeplayer so
  582. that monsters that see the impact will respond as if they had seen the
  583. player.
  584.  
  585. To avoid spending too much time, only a single client (or fakeclient) is
  586. checked each frame.  This means multi player games will have slightly
  587. slower noticing monsters.
  588. ============
  589. */
  590. float() FindTarget =
  591. {
  592.     local entity    client;
  593.     local float        r;
  594.  
  595. // if the first spawnflag bit is set, the monster will only wake up on
  596. // really seeing the player, not another monster getting angry
  597.  
  598. // spawnflags & 3 is a big hack, because zombie crucified used the first
  599. // spawn flag prior to the ambush flag, and I forgot about it, so the second
  600. // spawn flag works as well
  601.  
  602. //MED 10/17/96 added charmed stuff
  603.    if (self.charmed)
  604.       {
  605.       self.effects = self.effects | EF_DIMLIGHT;
  606.       if (self.huntingcharmer > 0)
  607.          {
  608.          UpdateCharmerGoal();
  609. //         self.goalentity = self.charmer;
  610.          r = vlen(self.origin - self.goalentity.origin);
  611.          if (r < MIN_CHARMER_DISTANCE)
  612.             {
  613. //            dprint("stopping\n");
  614.             if ((self.huntingcharmer == 3) && (r > TOOCLOSE_CHARMER_DISTANCE))
  615.                return FALSE;
  616. //            self.huntingcharmer = 0;
  617.             StopHuntingCharmer();
  618.             return TRUE;
  619.             }
  620.          }
  621.       else if (vlen (self.origin - self.charmer.origin) > MAX_CHARMER_DISTANCE)
  622.          {
  623. //         self.huntingcharmer = 1;
  624. //         dprint("hunting\n");
  625.          HuntCharmer();
  626.          return FALSE;
  627.          }
  628.       else if (vlen (self.origin - self.charmer.origin) < TOOCLOSE_CHARMER_DISTANCE)
  629.          {
  630. //         self.huntingcharmer = 1;
  631. //         dprint("fleeing\n");
  632.          FleeCharmer();
  633.          return FALSE;
  634.          }
  635.       }
  636.    if (sight_entity_time >= time - 0.1 && !(self.spawnflags & 3) && !(self.charmed) )
  637.       {
  638.         client = sight_entity;
  639.         if (client.enemy == self.enemy)
  640.             return;
  641.       }
  642. //MED 10/17/96 added charmed clause
  643.    else if (self.charmed)
  644.       {
  645.       local entity head;
  646.       local entity selected;
  647.       local float dist;
  648.  
  649.       selected = world;
  650.       dist = CHARMED_RADIUS;
  651.       head = findradius(self.origin, CHARMED_RADIUS);
  652.       while(head)
  653.          {
  654.          if(!(head.flags & FL_NOTARGET) && (head.flags & FL_MONSTER))
  655.             {
  656.             if (visible(head) && (visible_distance < dist) && (head.health>0))
  657.                {
  658.                if ((head !=self) && (head != self.charmer) && (head.charmer != self.charmer))
  659.                   {
  660.                   selected = head;
  661.                   dist = visible_distance;
  662.                   }
  663.                }
  664.             }
  665.          head = head.chain;
  666.          }
  667.       if (selected == world)
  668.          return FALSE;
  669.  
  670.       client = selected;
  671.       }
  672.    else
  673.     {
  674.         client = checkclient ();
  675.       if (!client)
  676.             return FALSE;    // current check entity isn't in PVS
  677.     }
  678.  
  679.     if (client == self.enemy)
  680.         return FALSE;
  681.  
  682.    //MED 10/17/96 added charmed stuff
  683.    if (!self.charmed)
  684.       {
  685.       if (client.flags & FL_NOTARGET)
  686.          return FALSE;
  687.       }
  688.    if (client.items & IT_INVISIBILITY)
  689.       return FALSE;
  690.  
  691.     r = range (client);
  692.     if (r == RANGE_FAR)
  693.         return FALSE;
  694.  
  695.     if (!visible (client))
  696.         return FALSE;
  697.  
  698.    //MED 10/17/96 added charmed stuff
  699.    if (!self.charmed)
  700.       {
  701.       if (r == RANGE_NEAR)
  702.          {
  703.          if (client.show_hostile < time && !infront (client))
  704.             return FALSE;
  705.          }
  706.       else if (r == RANGE_MID)
  707.          {
  708.          if ( /* client.show_hostile < time || */ !infront (client))
  709.             return FALSE;
  710.          }
  711.       }
  712.  
  713. //
  714. // got one
  715. //
  716.     self.enemy = client;
  717.  
  718. //MED 10/17/96 added charmed stuff
  719.    if ((!self.charmed) && (!self.enemy.charmed))
  720.       {
  721.       if (self.enemy.classname != "player")
  722.          {
  723.          self.enemy = self.enemy.enemy;
  724.          if (self.enemy.classname != "player")
  725.             {
  726.             self.enemy = world;
  727.             return FALSE;
  728.             }
  729.          }
  730.       }
  731.  
  732.     FoundTarget ();
  733.  
  734.     return TRUE;
  735. };
  736.  
  737.  
  738. //=============================================================================
  739.  
  740. void(float dist) ai_forward =
  741. {
  742.     walkmove (self.angles_y, dist);
  743. };
  744.  
  745. void(float dist) ai_back =
  746. {
  747.     walkmove ( (self.angles_y+180), dist);
  748. };
  749.  
  750.  
  751. /*
  752. =============
  753. ai_pain
  754.  
  755. stagger back a bit
  756. =============
  757. */
  758. void(float dist) ai_pain =
  759. {
  760.     ai_back (dist);
  761. /*
  762.     local float    away;
  763.  
  764.     away = anglemod (vectoyaw (self.origin - self.enemy.origin)
  765.     + 180*(random()- 0.5) );
  766.  
  767.     walkmove (away, dist);
  768. */
  769. };
  770.  
  771. /*
  772. =============
  773. ai_painforward
  774.  
  775. stagger back a bit
  776. =============
  777. */
  778. void(float dist) ai_painforward =
  779. {
  780.     walkmove (self.ideal_yaw, dist);
  781. };
  782.  
  783. /*
  784. =============
  785. ai_walk
  786.  
  787. The monster is walking it's beat
  788. =============
  789. */
  790. void(float dist) ai_walk =
  791. {
  792.     local vector        mtemp;
  793.  
  794.     movedist = dist;
  795.  
  796. //MED 01/20/97
  797.     // check for noticing a player
  798.    if (FindTarget ())
  799.       return;
  800.  
  801. //MED 11/02/96
  802.    if (self.huntingcharmer)
  803.       {
  804. //      movetogoal (dist*2);
  805.       movetogoal (dist);
  806.       self.nextthink = time + ((self.nextthink - time)/2);
  807.       }
  808.    else
  809.       movetogoal (dist);
  810. };
  811.  
  812.  
  813. /*
  814. =============
  815. ai_stand
  816.  
  817. The monster is staying in one place for a while, with slight angle turns
  818. =============
  819. */
  820. void() ai_stand =
  821. {
  822.    if (FindTarget ())
  823.       return;
  824.  
  825.     if (time > self.pausetime)
  826.     {
  827.         self.th_walk ();
  828.         return;
  829.     }
  830.  
  831. // change angle slightly
  832.  
  833. };
  834.  
  835. /*
  836. =============
  837. ai_turn
  838.  
  839. don't move, but turn towards ideal_yaw
  840. =============
  841. */
  842. void() ai_turn =
  843. {
  844.     if (FindTarget ())
  845.         return;
  846.  
  847.     ChangeYaw ();
  848. };
  849.  
  850. //MED 11/10/96 added ai_turn_in_place
  851. /*
  852. =============
  853. ai_turn_in_place
  854.  
  855. don't move, but turn towards ideal_yaw
  856. =============
  857. */
  858. void() ai_turn_in_place =
  859.    {
  860.    local float delta;
  861.  
  862.    self.nextthink = time + 0.1;
  863.    enemy_yaw = vectoyaw(self.enemy.origin - self.origin);
  864.    delta = fabs(self.angles_y - enemy_yaw);
  865.    if (delta > MIN_ANGLE_DELTA)
  866.       {
  867.       self.ideal_yaw = enemy_yaw;
  868.       ChangeYaw();
  869.       }
  870.    else
  871.       {
  872.       self.think = self.th_run;
  873.       }
  874.    };
  875.  
  876. //=============================================================================
  877.  
  878. /*
  879. =============
  880. ChooseTurn
  881. =============
  882. */
  883. void(vector dest3) ChooseTurn =
  884. {
  885.     local vector    dir, newdir;
  886.  
  887.     dir = self.origin - dest3;
  888.  
  889.     newdir_x = trace_plane_normal_y;
  890.     newdir_y = 0 - trace_plane_normal_x;
  891.     newdir_z = 0;
  892.  
  893.     if (dir * newdir > 0)
  894.     {
  895.         dir_x = 0 - trace_plane_normal_y;
  896.         dir_y = trace_plane_normal_x;
  897.     }
  898.     else
  899.     {
  900.         dir_x = trace_plane_normal_y;
  901.         dir_y = 0 - trace_plane_normal_x;
  902.     }
  903.  
  904.     dir_z = 0;
  905.     self.ideal_yaw = vectoyaw(dir);
  906. };
  907.  
  908. /*
  909. ============
  910. FacingIdeal
  911.  
  912. ============
  913. */
  914. float() FacingIdeal =
  915. {
  916.     local    float    delta;
  917.  
  918.     delta = anglemod(self.angles_y - self.ideal_yaw);
  919.     if (delta > 45 && delta < 315)
  920.         return FALSE;
  921.     return TRUE;
  922. };
  923.  
  924.  
  925. //=============================================================================
  926.  
  927. float()    WizardCheckAttack;
  928. float()    DogCheckAttack;
  929. //MED
  930. float()  GremlinCheckAttack;
  931. float()  ScourgeCheckAttack;
  932. float()  ArmagonCheckAttack;
  933.  
  934. float() CheckAnyAttack =
  935. {
  936.     if (!enemy_vis)
  937.         return;
  938.    if (self.classname == "monster_army")
  939.         return SoldierCheckAttack ();
  940.    if (self.classname == "monster_ogre")
  941.         return OgreCheckAttack ();
  942.    if (self.classname == "monster_shambler")
  943.         return ShamCheckAttack ();
  944.     if (self.classname == "monster_demon1")
  945.         return DemonCheckAttack ();
  946.     if (self.classname == "monster_dog")
  947.         return DogCheckAttack ();
  948.     if (self.classname == "monster_wizard")
  949.         return WizardCheckAttack ();
  950.    if (self.classname == "monster_gremlin")
  951.       return GremlinCheckAttack ();
  952.    if (self.classname == "monster_scourge")
  953.       return ScourgeCheckAttack ();
  954.    if (self.classname == "monster_armagon")
  955.       return ArmagonCheckAttack ();
  956.    return CheckAttack ();
  957. };
  958.  
  959.  
  960. /*
  961. =============
  962. ai_run_melee
  963.  
  964. Turn and close until within an angle to launch a melee attack
  965. =============
  966. */
  967. void() ai_run_melee =
  968. {
  969.     self.ideal_yaw = enemy_yaw;
  970.     ChangeYaw ();
  971.  
  972.     if (FacingIdeal())
  973.     {
  974.         self.th_melee ();
  975.         self.attack_state = AS_STRAIGHT;
  976.     }
  977. };
  978.  
  979.  
  980. /*
  981. =============
  982. ai_run_missile
  983.  
  984. Turn in place until within an angle to launch a missile attack
  985. =============
  986. */
  987. void() ai_run_missile =
  988. {
  989.     self.ideal_yaw = enemy_yaw;
  990.     ChangeYaw ();
  991.     if (FacingIdeal())
  992.     {
  993.         self.th_missile ();
  994.         self.attack_state = AS_STRAIGHT;
  995.     }
  996. };
  997.  
  998.  
  999. /*
  1000. =============
  1001. ai_run_slide
  1002.  
  1003. Strafe sideways, but stay at aproximately the same range
  1004. =============
  1005. */
  1006. void() ai_run_slide =
  1007. {
  1008.     local float    ofs;
  1009.  
  1010.     self.ideal_yaw = enemy_yaw;
  1011.     ChangeYaw ();
  1012.     if (self.lefty)
  1013.         ofs = 90;
  1014.     else
  1015.         ofs = -90;
  1016.  
  1017.     if (walkmove (self.ideal_yaw + ofs, movedist))
  1018.         return;
  1019.  
  1020.     self.lefty = 1 - self.lefty;
  1021.  
  1022.     walkmove (self.ideal_yaw - ofs, movedist);
  1023. };
  1024.  
  1025. //MED
  1026. /*
  1027. =============
  1028. ai_run_dodge
  1029.  
  1030. Strafe sideways, but continue moving towards the enemy
  1031. Used by the Scourge.
  1032. =============
  1033. */
  1034. void() ai_run_dodge =
  1035. {
  1036.     local float    ofs;
  1037.    local float newyaw;
  1038.  
  1039. /*
  1040. // attempt to jump over missiles
  1041.    if (self.enemy.weaponframe == 1)
  1042.       {
  1043.       if (self.flags & FL_ONGROUND)
  1044.          {
  1045.          self.origin_z = self.origin_z + 1;
  1046.          self.velocity = self.velocity + '0 0 500';
  1047.          self.flags = self.flags - FL_ONGROUND;
  1048.          }
  1049.       self.ltime = self.ltime + 1.0;
  1050.       }
  1051. */
  1052.    self.nextthink = time + 0.1;
  1053.    if (self.lefty)
  1054.       ofs = 40;
  1055.     else
  1056.       ofs = -40;
  1057.  
  1058.    if (time > self.ltime)
  1059.       {
  1060.       self.lefty = 1 - self.lefty;
  1061.       self.ltime = time + 0.8;
  1062.       }
  1063.  
  1064.    newyaw = enemy_yaw + ofs;
  1065.    self.ideal_yaw = enemy_yaw;
  1066.    if (walkmove (newyaw, movedist))
  1067.       {
  1068.       ChangeYaw ();
  1069.       return;
  1070.       }
  1071.  
  1072.    self.lefty = 1 - self.lefty;
  1073.    self.ltime = time + 0.8;
  1074.    newyaw = enemy_yaw - ofs;
  1075.    self.ideal_yaw = enemy_yaw;
  1076.    walkmove (newyaw, movedist);
  1077.    ChangeYaw ();
  1078. };
  1079.  
  1080. /*
  1081. =============
  1082. ai_run
  1083.  
  1084. The monster has an enemy it is trying to kill
  1085. =============
  1086. */
  1087. float RUN_STRAIGHT;
  1088. void(float dist) ai_run =
  1089. {
  1090.     local    vector    delta;
  1091.     local    float    axis;
  1092.     local    float    direct, ang_rint, ang_floor, ang_ceil;
  1093.  
  1094.     movedist = dist;
  1095. // see if the enemy is dead
  1096.    if (self.enemy.health <= 0 || (self.charmed && (self.charmer == self.enemy)))
  1097.     {
  1098.         self.enemy = world;
  1099. //MED 10/30/96 added charmed stuff
  1100.       if (self.charmed)
  1101.          {
  1102.          HuntCharmer();
  1103.          return;
  1104.          }
  1105.     // FIXME: look all around for other targets
  1106.         if (self.oldenemy.health > 0)
  1107.         {
  1108.             self.enemy = self.oldenemy;
  1109.             HuntTarget ();
  1110.         }
  1111.         else
  1112.         {
  1113.             if (self.movetarget)
  1114.                 self.th_walk ();
  1115.             else
  1116.                 self.th_stand ();
  1117.             return;
  1118.         }
  1119.     }
  1120.  
  1121.     self.show_hostile = time + 1;        // wake up other monsters
  1122.  
  1123. // check knowledge of enemy
  1124.     enemy_vis = visible(self.enemy);
  1125.     if (enemy_vis)
  1126.         self.search_time = time + 5;
  1127.  
  1128. // look for other coop players
  1129. //MED 10/17/96 added charmed stuff
  1130.    if (coop && self.search_time < time && !self.charmed)
  1131.     {
  1132.         if (FindTarget ())
  1133.             return;
  1134.     }
  1135.     enemy_infront = infront(self.enemy);
  1136.     enemy_range = range(self.enemy);
  1137.     enemy_yaw = vectoyaw(self.enemy.origin - self.origin);
  1138.  
  1139. //MED
  1140.    if (self.th_turn)
  1141.       {
  1142.       local float angledelta;
  1143.  
  1144.       angledelta = fabs(self.angles_y - enemy_yaw);
  1145.       if (angledelta > MIN_ANGLE_DELTA)
  1146.          {
  1147.          self.th_turn();
  1148.          return;
  1149.          }
  1150.       }
  1151.  
  1152.     if (self.attack_state == AS_MISSILE)
  1153.     {
  1154. //dprint ("ai_run_missile\n");
  1155.         ai_run_missile ();
  1156.         return;
  1157.     }
  1158.     if (self.attack_state == AS_MELEE)
  1159.     {
  1160. //dprint ("ai_run_melee\n");
  1161.         ai_run_melee ();
  1162.         return;
  1163.     }
  1164.  
  1165.     if (CheckAnyAttack ())
  1166.       {
  1167.       return;              // beginning an attack
  1168.       }
  1169.  
  1170.     if (self.attack_state == AS_SLIDING)
  1171.     {
  1172.         ai_run_slide ();
  1173.         return;
  1174.     }
  1175.  
  1176.    if (self.attack_state == AS_DODGING)
  1177.     {
  1178.       ai_run_dodge ();
  1179.         return;
  1180.     }
  1181.  
  1182. //MED 11/11/96
  1183.    if (RUN_STRAIGHT && time > self.endtime)
  1184.       {
  1185.       RUN_STRAIGHT = 0;
  1186.       axis = walkmove (self.angles_y, movedist);
  1187.       if (!axis)
  1188.          {
  1189.          self.endtime = time + 3;
  1190.          movetogoal (dist);      // done in C code...
  1191.          }
  1192. //      else
  1193. //         ChangeYaw();
  1194.       }
  1195.    else
  1196.       {
  1197.       // head straight in
  1198.       movetogoal (dist);      // done in C code...
  1199.       }
  1200. };
  1201.